home *** CD-ROM | disk | FTP | other *** search
- /*
- Copyright Cornell University 1986. All rights are reserved.
-
- 3270token.c consists of token processing, keyboard mapping
- & translation routines for the IBM 3270 emulator
- */
-
-
- #include <em.h>
-
- #include <3270.h>
- #include <rcodes.h>
-
- #include <macdefs.h>
- #include <h19.h>
-
- #define IBMPASTEMODE 1 /* paste, abort on protected field entry */
- #define IBMPASTETAB 2 /* paste, try skipping protected field on char entry */
- #define IBMPASTEABORT 3
-
- int pastemode = FALSE; /* flag for ibmsendchar() to abort on protected */
-
- extern unsigned char pfkeymap(); /* forward declaration */
-
- ibmproc_token(tkptr)
- struct token *tkptr;
- {
- unsigned char *ptr;
- extern int icon_up;
- static int nselystart = 0;
- static int nselxstart = 0;
- static int nselyend = 0;
- static int nselxend = 0;
-
- #ifdef ICONLOCKS
- if (icon_up && !hycactive) {
- /* allow no input if the window has been shrunk--unless tndrvr running */
- beep();
- return(0);
- }
- #endif
-
- if (emdp->event_reg & KYBD_LOCK) {
- if ((tkptr->class == RSLT_LCAC) && (tkptr->entry == RESET))
- /* fall through to do it */
- flushactions(emdp); /* kill all actions remaining */
- else {
- beep();
- return(0);
- }
- }
- if (emdp->event_reg & TFTP_ON)
- {
- if ((tkptr->class == RSLT_PFKY) && (tkptr->entry == PA1))
- ft_usr();
- return(-1);
- }
-
- switch(tkptr->class) {
- case RSLT_ASCI: {
- /* ascii character */
- if (emdp->event_reg & SYS_LOCK)
- return(-1);
-
- ibmsendchar(tkptr->entry);
- break;
- }
- case RSLT_EDIT: {
- if (stdedit(tkptr->entry)) {
- /* stdedit didn't handle the item */
- if (emdp->event_reg & SYS_LOCK)
- return(-1);
-
- if (tkptr->entry == ED_PASTE) {
- /* take the current scrap & feed it into proc_token? */
- long scraplen;
- unsigned char * thep;
- unsigned char thechar;
-
- scraplen = gettextscrap(&thep);
-
- /* paste w/ respect for 3270 limitations */
- pastemode = IBMPASTEMODE; /* set flag for sendchar */
- while (scraplen-- > 0) {
- if ((thechar = *thep++) == CR) {
- /* do a Line Feed when we see a CR */
- ibmmvcurs(NEW_LINE);
- setibmcursor();
- }
- else if (thechar == TAB) {
- /* make four spaces for a TAB ? */
- ibmsendchar(' ');
- ibmsendchar(' ');
- ibmsendchar(' ');
- ibmsendchar(' ');
- }
- else if (thechar == PT) {
- /* do a 3270 TAB */
- ibmmvcurs(TAB_FWD);
- setibmcursor();
- }
- else if (thechar == CTLB) {
- /* go ahead and TAB to new fields rather than aborting */
- pastemode = IBMPASTETAB;
- }
- else {
- ibmsendchar(thechar);
- }
- if (pastemode == IBMPASTEABORT) {
- /* abort on error */
- beep();
- break;
- }
- }
- releasescrap();
- pastemode = FALSE; /* set flag for sendchar */
- break;
- }
- else
- entryerr(tkptr);
- }
- break;
- }
- case RSLT_PFKY: {
- /* program function key, enter, clear */
- if (emdp->event_reg & SYS_LOCK)
- return(-1);
-
- switch (tkptr->entry) {
- case PFSHIFT: {
- if (emdp->pfdubshift)
- return(0);
- if (!emdp->pfshift) {
- emdp->pfshift = TRUE;
- pfshowshift();
- }
- else {
- emdp->pfshift = FALSE;
- pfclrshift();
- }
- return(0);
- }
- case PFDUBSHIFT: {
- if (emdp->pfshift)
- return(0);
- if (emdp->pfdubshift) {
- emdp->pfdubshift = TRUE;
- pfshowdubshift();
- }
- else {
- emdp->pfdubshift = FALSE;
- pfclrdubshift();
- }
- return(0);
- }
- case PF1:
- case PF2:
- case PF3:
- case PF4:
- case PF5:
- case PF6:
- case PF7:
- case PF8:
- case PF9:
- case PF10:
- case PF11:
- case PF12: {
- /* a pf key should be sent, check for shift */
- if (emdp->pfshift) {
- emdp->pfshift = FALSE;
- emdp->pfdubshift = FALSE;
- pfclrshift();
- pfclrdubshift();
- tkptr->entry += 12;
- }
- else if (emdp->pfdubshift) {
- emdp->pfshift = FALSE;
- emdp->pfdubshift = FALSE;
- pfclrshift();
- pfclrdubshift();
- tkptr->entry += 24;
- }
- break;
- }
- default: {
- break;
- /* fall through to send others-- PA1, Enter, etc. */
- }
- }
- ibmsendpf(tkptr->entry);
- return(0);
- }
- case RSLT_MVCR: {
- /* cursor movement key */
- if (emdp->event_reg & SYS_LOCK)
- return(-1);
-
- if (ibmmvcurs((short) tkptr->entry))
- entryerr(tkptr);
- else
- /* set the ibm cursor to the new position */
- setibmcursor();
- return(0);
- }
- case RSLT_YCURS: {
- /* move to beginning of line y */
- if (emdp->event_reg & SYS_LOCK)
- return(-1);
-
- cursor_ptr = scr_map + ((unsigned int) tkptr->entry * emdp->linelength);
- if (cursor_ptr >= smap_end)
- cursor_ptr = scr_map;
-
- /* fix current attribute */
- if (*cursor_ptr < ATTR)
- current_attr = cursor_ptr;
- else {
- if ((ptr = look_attr(cursor_ptr,BACK)) != NULL)
- current_attr = ptr;
- }
- newibmcursor();
- return(0);
- }
- case RSLT_XCURS: {
- /* move to x on current line */
- int curxpos;
-
- if (emdp->event_reg & SYS_LOCK)
- return(-1);
-
- curxpos = tkptr->entry - ((cursor_ptr - scr_map) % emdp->linelength);
- cursor_ptr += curxpos;
- /* current y offset + x given in entry */
- if (cursor_ptr >= smap_end)
- cursor_ptr = scr_map;
- if (*cursor_ptr < ATTR)
- current_attr = cursor_ptr;
- else {
- if ((ptr = look_attr(cursor_ptr,BACK)) != NULL)
- current_attr = ptr;
- }
- setibmcursor();
- return(0);
- }
- case RSLT_LCAC: {
- /* local action key */
- if (emdp->event_reg & SYS_LOCK)
- return(-1);
-
- if (localkey(tkptr->entry))
- entryerr(tkptr);
- break;
- }
- default: {
- if (stdtoken(tkptr))
- classerr(tkptr);
- return(0);
- }
- }
- emdp->event_reg &= ~CLEAR_KEY; /* turn off clear flag */
- return(0);
- }
-
-
-
- unsigned char
- pfkeymap(entrycode)
- unsigned char entrycode;
- {
- switch (entrycode) {
- case PF1:
- return(RPF1);
- case PF2:
- return(RPF2);
- case PF3:
- return(RPF3);
- case PF4:
- return(RPF4);
- case PF5:
- return(RPF5);
- case PF6:
- return(RPF6);
- case PF7:
- return(RPF7);
- case PF8:
- return(RPF8);
- case PF9:
- return(RPF9);
- case PF10:
- return(RPF10);
- case PF11:
- return(RPF11);
- case PF12:
- return(RPF12);
- case PF13:
- return(RPF13);
- case PF14:
- return(RPF14);
- case PF15:
- return(RPF15);
- case PF16:
- return(RPF16);
- case PF17:
- return(RPF17);
- case PF18:
- return(RPF18);
- case PF19:
- return(RPF19);
- case PF20:
- return(RPF20);
- case PF21:
- return(RPF21);
- case PF22:
- return(RPF22);
- case PF23:
- return(RPF23);
- case PF24:
- return(RPF24);
- case PA1:
- return(RPA1);
- case PA2:
- return(RPA2);
- case PA3:
- return(RPA3);
- case CLEAR:
- return(RCLEAR);
- case ENTER:
- return(RENTER);
- case PFSHIFT:
- return(0);
- case PFDUBSHIFT:
- return(0);
- }
- return(0);
- }
-
-
- doreset()
- {
- emdp->event_reg &= ~INSERT;
- emdp->event_reg &= ~KYBD_LOCK;
- emdp->event_reg &= ~SYS_LOCK;
-
- /* reset the flags */
- clrinsert();
- clrkbdlock();
- clrsyslock();
- }
-
-
- /* TODO this stuff hereafter belongs in an output.c module */
-
- ibmsendchar(thechar)
- unsigned char thechar;
- {
- register unsigned char *ptr;
- register unsigned char *prevptr;
- int cnt = 0;
- int count;
-
- if (!emdp->connopen)
- return(-1);
-
- if (pastemode == IBMPASTEABORT)
- return(-1);
-
- if (thechar < 32 || thechar == 0xFF)
- /* invalid char, < space had (|| thechar >= 127) and >= DEL */
- return(-1);
-
- if ((current_attr == NULL)
- || ((*cursor_ptr >= ATTR) && (!(*current_attr & PROT))))
- {
- /* we're in an unprotected field, place the new char in it */
-
- if (!(emdp->event_reg & INSERT)) {
- /* standard mode, overwrite current character */
- *cursor_ptr = thechar;
- if (pastemode) {
- /* wait for caller to draw */
- set_mod(cursor_ptr, 1);
- }
- else {
- (*emdp->ch_draw)(thechar, pc_attr());
- }
- }
- else {
- /* we're in insert mode */
- /* count non-nulls remaining in field */
- for (ptr = cursor_ptr; *ptr >= ATTR && *ptr != IBMNULL; cnt++) {
- if (++ptr == smap_end)
- ptr = scr_map;
- }
- if (*ptr < ATTR) {
- if (emdp->squeezeblanks) {
- if (ptr == scr_map)
- ptr = smap_end - 1;
- else
- --ptr;
- if (cnt > 1 && *ptr == ' ') {
- /* let's squeeze out this trailing blank */
- --cnt;
- }
- else {
- /* failure */
- if (pastemode)
- pastemode = IBMPASTEABORT;
- else
- flushactions(emdp); /* kill all actions on KYBD_LOCK */
- #ifdef PROTLOCK
- emdp->event_reg |= KYBD_LOCK;
- showkbdlock();
-
- #else
- /* beep instead of locking the keyboard */
- beep();
- #endif
- return(0);
- }
- }
- else {
- if (pastemode)
- pastemode = IBMPASTEABORT;
- else
- flushactions(emdp); /* kill all actions on KYBD_LOCK */
- #ifdef PROTLOCK
- emdp->event_reg |= KYBD_LOCK;
- showkbdlock();
- #else
- beep();
- #endif
- return(0);
- }
- }
- prevptr = ptr - 1;
- cnt++;
-
- /* we are counting backwards on the screen */
- for (count = cnt - 1; count > 0; --ptr, --prevptr, --count) {
- if (ptr < scr_map)
- ptr += emdp->screensize;
- if (prevptr < scr_map) {
- /* wrap around to the bottom of the screen */
- prevptr += emdp->screensize;
- *ptr = *prevptr;
- if (pastemode) {
- /* wait to draw */
- set_mod(scr_map, cnt - count);
- }
- else {
- /* put out what so far */
- (*emdp->str_draw)(scr_map, cnt - count, pc_attr());
- }
- cnt = count;
- }
- else
- *ptr = *prevptr; /* normal squeeze */
- }
- if (cnt) {
- *cursor_ptr = thechar;
- if (pastemode) {
- /* wait to draw */
- set_mod(cursor_ptr, cnt);
- }
- else {
- (*emdp->str_draw)(cursor_ptr, cnt, pc_attr());
- }
- }
- }
-
- if (current_attr)
- *current_attr |= MDT;
- if (++cursor_ptr == smap_end)
- cursor_ptr = scr_map;
- if (*cursor_ptr < ATTR) {
- /* find next unprotected field */
-
- if (pastemode == IBMPASTEMODE) {
- /* when pasting into 3270 screen, hitting a new field aborts */
- pastemode = IBMPASTEABORT;
- }
- cursor_ptr = next_unp(FWD);
- if ((current_attr = cursor_ptr - 1) < scr_map)
- current_attr += emdp->screensize;
- }
- if (pastemode) {
- newibmcursor();
- emdp->event_reg |= SCREEN_EVENT;
- /* make sure new chars get drawn */
- }
- else
- setibmcursor();
- }
- else {
- /* protected field is a no-no */
- if (pastemode == IBMPASTEMODE)
- pastemode = IBMPASTEABORT;
- else
- flushactions(emdp); /* kill all actions on KYBD_LOCK */
- #ifdef PROTLOCK
- emdp->event_reg |= KYBD_LOCK;
- showkbdlock();
- #else
- beep();
- #endif
- }
- }
-
-
- /* for compatibility with c19 file transfer */
-
- ibmsendftchar(tchar)
- unsigned char tchar;
- {
- ibmsendchar(tchar);
- }
-
-
- /* send a string, in this case to the internal 3270 translator */
-
- ibmsendstr(strp)
- register unsigned char * strp;
- {
- while (*strp)
- ibmsendchar(*strp++);
- }
-
-
- /* send a pf key, in this case to the internal 3270 translator */
-
- ibmsendpf(pfkey)
- unsigned char pfkey;
- {
- unsigned char tkey;
-
- if (!emdp->connopen)
- return(-1);
-
- if ( !(tkey = pfkeymap(pfkey))) /* save it */
- return(-1);
-
- emdp->aid_key = tkey;
-
- emdp->event_reg |= SYS_LOCK;
- showsyslock();
- if (emdp->event_reg & INSERT) {
- /* turn off insert if it's on */
- clrinsert();
- emdp->event_reg &= ~INSERT;
- }
-
- read_mod(); /* telnet server expects this */
-
- savescreen();
- /* save the screen before it gets overwritten */
-
- if (pfkey == CLEAR) {
- /* erase screen */
-
- emdp->event_reg |= CLEAR_KEY; /* set CLEAR-encountered flag */
- cursor_ptr = scr_map;
- current_attr = NULL;
- mem_clear(scr_map,emdp->screensize,(char) IBMNULL);
- (*emdp->clear_scr)();
- }
-
- if (!emdp->typeahead)
- flushactions(emdp); /* kill all actions on SYS_LOCK if typeahead off */
-
- return(-1);
- }
-
-
- localkey(code)
- unsigned char code;
- {
- register unsigned char * ptr;
- register unsigned char * nextattr;
- register unsigned char * nextp;
- unsigned char *stake;
- register int cnt;
-
- switch (code) {
- case INSRT: {
- emdp->event_reg ^= INSERT;
- if (emdp->event_reg & INSERT)
- showinsert();
- else
- clrinsert();
- break;
- }
- case ERASE_EOF: {
- if ((current_attr == NULL) || !(*current_attr & PROT)) {
- if (current_attr != NULL)
- /* if formatted buffer stop at attr */
- nextattr = look_attr(cursor_ptr, FWD);
- else
- /* go to end of screen*/
- nextattr = smap_end;
- stake = ptr = cursor_ptr;
- for (cnt = 0; ptr != nextattr; ) {
- if (ptr == smap_end) {
- ptr = scr_map;
- continue;
- }
- *ptr++ = IBMNULL;
- cnt++;
- }
- if (cnt) {
- (*emdp->str_draw)(stake, cnt, pc_attr());
- if (current_attr != NULL) /* if formatted buffer */
- *current_attr |= MDT; /* turn on MDT */
- }
- }
- else {
- /* protected field */
- flushactions(emdp); /* kill all actions on KYBD_LOCK */
- #ifdef PROTLOCK
- emdp->event_reg |= KYBD_LOCK;
- showkbdlock();
-
- #else
- beep();
- #endif
- }
- break;
- }
- case INPUT_ERASE: {
- if (current_attr != NULL) {
- eau(); /* erase all unprotected */
- }
- else {
- cursor_ptr = scr_map;
- modflg = emdp->modflg = (long) SCRALLMOD;
- for (ptr = scr_map; ptr < smap_end; )
- *ptr++ = IBMNULL;
- }
- emdp->event_reg |= SCREEN_EVENT;
- setibmcursor();
- break;
- }
- case BACKSP_BLANK: {
- /* first backspace */
- unsigned char * curstmp;
- unsigned char * atttmp;
-
- if (*cursor_ptr < ATTR)
- /* new attribute */
- if ((ptr = look_attr(cursor_ptr, BACK)) != NULL)
- current_attr = ptr;
- if (--cursor_ptr < scr_map)
- cursor_ptr += emdp->screensize;
-
- newibmcursor();
- if (! (*cursor_ptr < ATTR)) {
- if (current_attr == NULL || !(*current_attr & PROT)) {
- /* OK to blank out unprotected char */
-
- curstmp = cursor_ptr;
- atttmp = current_attr;
-
- ibmsendchar(' ');
-
- /* go back again to where we were */
- cursor_ptr = curstmp;
- current_attr = atttmp;
- }
- }
- setibmcursor();
- break;
- }
- case BACKSP_DEL: {
- if (*cursor_ptr < ATTR)
- if ((ptr = look_attr(cursor_ptr, BACK)) != NULL)
- current_attr = ptr;
- if (--cursor_ptr < scr_map)
- cursor_ptr += emdp->screensize;
- newibmcursor();
- /* fall through to do a delete */
- }
- case DEL_CHAR: {
- /* doesn't work on an unformatted buffer--??? kev */
-
- int ymod;
- int ylast;
-
- if ((current_attr != NULL)
- && (*cursor_ptr >= ATTR)
- && !(*current_attr & PROT))
- {
- nextattr = look_attr(cursor_ptr, FWD);
- ptr = nextp = stake = cursor_ptr;
-
- if (!emdp->shiftfield) {
- /* delete only to next attr or end of line, standard behavior */
- if (nextattr < ptr || (nextattr - scr_map) / emdp->linelength != ypos)
- nextattr = scr_map + emdp->linelength * (ypos + 1);
-
- for (cnt = 0; ptr != nextattr; cnt++, ptr++) {
- if ((ptr + 1) != nextattr)
- *ptr = *(ptr + 1); /* march left */
- else
- *ptr = IBMNULL; /* or pad with nulls */
- }
- /* display results now */
- if (cnt) {
- (*emdp->str_draw)(stake, cnt, pc_attr());
- *current_attr |= MDT; /* turn on MDT */
- }
- }
- else {
- /* shift whole field over */
- if (++nextp >= smap_end)
- nextp = scr_map;
- while (nextp != nextattr) {
- *ptr = *nextp; /* shift left */
- if (++ptr >= smap_end)
- ptr = scr_map;
- if (++nextp >= smap_end)
- nextp = scr_map;
- }
- *ptr = IBMNULL; /* pad with null at end */
- *current_attr |= MDT; /* turn on MDT */
-
- /* update display */
- if (nextattr < stake) {
- /* wrapped, must do 2 set_mods */
- set_mod(stake, smap_end - stake);
- set_mod(scr_map, nextattr - scr_map);
- }
- else {
- set_mod(stake, nextattr - stake);
- }
- emdp->event_reg |= SCREEN_EVENT;
- }
- }
- else
- {
- /* we're in a protected field */
- flushactions(emdp); /* kill all actions on KYBD_LOCK */
- #ifdef PROTLOCK
- emdp->event_reg |= KYBD_LOCK;
- showkbdlock();
- #else
- beep();
- #endif
- }
- break;
- }
- case RESET: {
- doreset();
- break;
- }
- default: {
- return(TRUE);
- }
- }
- return(FALSE);
- }
-
-
-
- ibmmvcurs(entry)
- unsigned short entry;
- {
- register int cnt;
- unsigned char *ptr;
-
- switch (entry) {
- case LEFT_ARROW: {
- if (*cursor_ptr < ATTR) /* move the cursor to the left */
- if ((ptr = look_attr(cursor_ptr,BACK)) != NULL)
- current_attr = ptr;
- if (--cursor_ptr < scr_map)
- cursor_ptr += emdp->screensize;
- break;
- }
- case RIGHT_ARROW: {
- if (++cursor_ptr >= smap_end) /* move the cursor to */
- cursor_ptr -= emdp->screensize; /* the right wrap around*/
- if (*cursor_ptr < ATTR) {
- current_attr = cursor_ptr;
- }
- break;
- }
- case UP_ARROW: {
- if ((cursor_ptr -= emdp->linelength) < scr_map)/* move the cursor */
- cursor_ptr += emdp->screensize; /* up one line - wrap if*/
- if (*cursor_ptr < ATTR) /* necessary also reset*/
- current_attr = cursor_ptr; /* the current attribute*/
- else /* field */
- if ((ptr = look_attr(cursor_ptr,BACK)) != NULL)
- current_attr = ptr;
- break;
- }
- case DOWN_ARROW: {
- if ((cursor_ptr += emdp->linelength) >= smap_end)/* move the */
- cursor_ptr -= emdp->screensize; /* down one line wrap if*/
- if (*cursor_ptr < ATTR) /* necessary also reset*/
- current_attr = cursor_ptr; /* the current attribute*/
- else /* field */
- if ((ptr = look_attr(cursor_ptr,BACK)) != NULL)
- current_attr = ptr;
- break;
- }
- case HOME: {
- cursor_ptr = smap_end - 1;
- if ((cursor_ptr = next_unp (FWD)) == (smap_end - 1))
- cursor_ptr = scr_map;
- if (current_attr != NULL)
- if ((current_attr = cursor_ptr - 1) < scr_map)
- current_attr += emdp->screensize;
- break;
- }
- case NEW_LINE: {
- cnt = (((cursor_ptr - scr_map) / emdp->linelength) + 1) * emdp->linelength;
- if (cnt == emdp->screensize)
- cnt = 0;
- cursor_ptr = scr_map + cnt;
- if (*cursor_ptr >= ATTR) {
- /* if not attribute, find next attrib */
- if ((current_attr = look_attr(cursor_ptr,BACK)) == NULL)
- break;
- if (! (*current_attr & PROT))
- /* not protected, we don't need to skip to next field */
- break;
- if (pastemode) {
- /* protected, abort pastemode when we land in protected field */
- pastemode = IBMPASTEABORT; /* abort pastemode */
- break;
- }
- }
- else if (pastemode && (*cursor_ptr & PROT)) {
- /* abort pastemode if we land in protected field */
- current_attr = cursor_ptr;
- pastemode = IBMPASTEABORT; /* abort pastemode */
- break;
- }
- /* else fall through & set on next unprotected */
- }
- case TAB_FWD:
- case BACK_TAB: {
- cursor_ptr = next_unp((entry == BACK_TAB) ? BACK : FWD);
- if ((current_attr = cursor_ptr - 1) < scr_map)
- current_attr += emdp->screensize;
- if (*current_attr >= ATTR || (*current_attr & PROT)) {
- cursor_ptr = scr_map;
- current_attr = look_attr(scr_map,BACK);
- }
- break;
- }
- default: {
- return(TRUE);
- }
- }
- return(FALSE);
- }
-
-
-